package com.scsj.cloud.authority.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.scsj.cloud.authority.model.SysUser;
import com.scsj.cloud.authority.service.SysUserService;
import com.scsj.cloud.base.rest.RestResponse;
import com.scsj.cloud.base.util.RestUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/scsj/authority/user")
@Api(value = "用户相关")
public class SysUserController {
	
	@Autowired
	private SysUserService userService;
	
	@PostMapping(value = "/insert")
	@ApiOperation(value = "新增单个用户", notes = "参数：用户对象")
	public RestResponse insert(@RequestBody SysUser sysUser) {
		int i = userService.insert(sysUser);
		if(i > 0) {
			
			return RestUtils.returnSuccess("新增成功 ！"); 
		}
		return RestUtils.returnFailure("新增失败 ！");
	}
	
	@GetMapping(value = "/findOne")
	@ApiOperation(value = "查询单个用户", notes = "参数：用户ID")
	public RestResponse findOne(@RequestParam int userId) {
		SysUser sysUser = userService.findOne(userId);
		return RestUtils.returnSuccess(sysUser);
	}	

	@GetMapping(value = "/findAll")
	@ApiOperation(value = "查询全部用户", notes = "参数：无")
	public RestResponse findAll() {
		List<SysUser> userList = userService.findAll();
		return RestUtils.returnSuccess(userList);
	}
	
	@DeleteMapping(value = "/delete")
	@ApiOperation(value = "删除单个用户", notes = "参数：用户ID")
	public RestResponse delete(@RequestParam int userId) {
		int i = userService.delete(userId);
		if(i > 0) {
			return RestUtils.returnSuccess("删除成功 ！");
		}
		return RestUtils.returnFailure("删除失败 ！");
	}
	
	@PutMapping(value = "/update")
	@ApiOperation(value = "修改单个用户", notes = "参数：用户对象")
	public RestResponse update(@RequestBody SysUser sysUser) {
		int i = userService.update(sysUser);
		if(i > 0) {
			return RestUtils.returnSuccess("修改成功 ！");
		}
		return RestUtils.returnFailure("修改失败 ！");
	}
	
	
}
